package com.woniuxy.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.commons.request.ResPage;
import com.woniuxy.user.dto.EmployeeDto;
import com.woniuxy.user.dto.UserInfoDto;
import com.woniuxy.user.entity.User;
import com.woniuxy.user.mapper.UserMapper;
import com.woniuxy.user.service.DepartmentService;
import com.woniuxy.user.service.UserService;
import com.woniuxy.user.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author: Mr·Xiang
 * @create 2024-04-07 9:47
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private DepartmentService departmentService;
    @Resource
    private BCryptPasswordEncoder passwordEncoder;
    @Override
    public UserInfoDto findById(Integer id) {
        return userMapper.findById(id);
    }

    @Override
    public Boolean checkPass(LoginVo loginVo) {
        User user = userMapper.findByAccount(loginVo.getAccount());
        if (user != null) {
            return passwordEncoder.matches(loginVo.getPassword(), user.getPassword());
        }
        return false;
    }

    @Override
    public Boolean updatePassword(LoginVo loginVo) {
        loginVo.setPassword(passwordEncoder.encode(loginVo.getPassword()));
        return userMapper.updatePassword(loginVo) > 0;
    }

    @Override
    public ResPage<EmployeeDto> getEmployeeList(EmployeeVo employeeVo) {
        PageHelper.startPage(employeeVo.getPage(), employeeVo.getSize());
        List<EmployeeDto> list = userMapper.getEmployeeList(employeeVo);
        PageInfo<EmployeeDto> pageInfo = new PageInfo<>(list);
        return new ResPage<>(pageInfo);
    }

    @Transactional
    @Override
    public Boolean addEmployee(AddEmployeeVo addEmployeeVo) {
        //对密码进行编码
        addEmployeeVo.setPassword(passwordEncoder.encode(addEmployeeVo.getPassword()));
        //添加用户信息到用户表
        userMapper.addEmployee(addEmployeeVo);
        //添加用户信息到用户角色表
        userMapper.addUserRole(addEmployeeVo);
        //变更对应部门的成员数量
        return departmentService.increaseDepartmentNum(addEmployeeVo.getDid()) == 1;
    }
    @Transactional
    @Override
    public Boolean editEmployee(AddEmployeeVo addEmployeeVo) {
        //修改用户表
        userMapper.editEmployee(addEmployeeVo);
        //变更对应部门的成员数量
        if (!Objects.equals(addEmployeeVo.getDid(), addEmployeeVo.getOldDid())){
            departmentService.increaseDepartmentNum(addEmployeeVo.getDid());
            departmentService.decreaseDepartmentNum(addEmployeeVo.getOldDid());
        }
        //修改用户角色表
        return userMapper.editUserRole(addEmployeeVo) > 0;
    }

    @Override
    public Boolean checkUname(String uname) {
        return userMapper.checkUname(uname) == 0;
    }

    @Override
    public Boolean enableAccount(Integer id) {
        return userMapper.enableAccount(id) > 0;
    }

    @Override
    public Boolean disableAccount(Integer id) {
        return userMapper.disableAccount(id) > 0;
    }

    @Transactional
    @Override
    public Boolean deleteEmployee(Integer id, Integer did) {
        userMapper.deleteEmployee(id);
        //变更对应部门的成员数量
        return departmentService.decreaseDepartmentNum(did) == 1;
    }

    @Override
    public BGVo findBorrowerAndGuarantee() {
        BGVo bgVo = new BGVo();
        /**获取借款人list*/
        List<User> borrowerList = userMapper.findBorrower();
        bgVo.setBorrowerList(borrowerList);
        /**获取担保人list*/
        List<User> guaranteeList = userMapper.findGuarantee();
        bgVo.setGuaranteeList(guaranteeList);
        return bgVo;
    }

    @Override
    @Transactional
    public Boolean registerConsumer(RegisterVo registerVo) {
        AddEmployeeVo addEmployeeVo = new AddEmployeeVo(registerVo);
        //添加到user以及关联表
        addEmployee(addEmployeeVo);
        //添加到user_details表
        registerVo.setId(addEmployeeVo.getId());
        return userMapper.addUserDetails(registerVo) > 0;
    }
}
